<template>
  <div style="height: 100%" v-loading="loading">
    <van-nav-bar title="销售出库信息" left-text="返回" left-arrow @click-left="goBack" />
    <van-cell-group :v-model="tableData" style="
          overflow: scroll;
          height: 520px;
          ::-webkit-scrollbar {
            display: none;
          }
        ">
      <van-row>
        <van-field v-model="tableData.deliverydocument" label="送货单号" readonly/>
      </van-row>
      <van-row>
        <van-field v-model="tableData.customername" label="客户名称" readonly/>
      </van-row>
      <van-row>
        <van-field v-model="tableData.totalquantity" label="合计数量" readonly />
      </van-row>
      <van-field style="border: solid 1px #3793fb; border-radius: 5px" v-model="code_input" ref="code_input"
                 placeholder="请扫描桶号条码" left-icon="scan" @input="onScanCode" />
      <van-swipe width="100%" ref="swiper" style="border: solid #3793fb 1px;border-top:none">
        <van-swipe-item v-for="(item, index) in tableData.bucket" :key="index">
          <van-field v-model="item.numberOfBarrels" label="桶号：" @input="quantityChange" ></van-field>
        </van-swipe-item>
      </van-swipe>
        <van-field v-model="tableData.info" label="提示信息" readonly />
    </van-cell-group>
    <van-button @click="dialogVisible = true" style="
          position: fixed;
          bottom: 0px;
          border: #3793fb solid 1px;
          border-radius: 2px;
          background-color: #3793fb;
          line-height: var(--footer-height);
        " type="primary" block>销售出库确认</van-button>
    <el-dialog
        title="是否销售出库"
        :visible.sync="dialogVisible"
        width="70%">
      <span>确定进行销售出库?</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="onSubmit">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: {
        bucket: [
        ],
      },
      loading: false,
      code_input: '',
    };
  },
  mounted() {
    //销售出库信息
    this.getSalesOutboundList(this.$route.query.codeInfo);
  },
  methods: {
    async getSalesOutboundList(codeInfo) {
      let scanInfo = {
        code: codeInfo + "," + localStorage.getItem("plant"),
      };
      const { data: res } = await this.$http.post(
          "/GetProductionInboundList",
          scanInfo
      );
      console.log(res[0]);
      this.tableData = res[0];
      this.tableData.bucket = [];
    },
    //返回
    goBack() {
      this.$router.push({
        path: '/ScanView',
        query: { title: '销售出库扫描', key: 6 }
      });
    },
    onScanCode(event) {
      this.getData(event);
    },
    async getData(codeInput) {
      let scanInfo = {
        code: codeInput,
      };
      const { data: res } = await this.$http.post(
          "/ProductionVerifyBucket",
          scanInfo
      );
      if (res[0].type == "E") {
        this.$message({
          message: res[0].message,
          type: "error",
        });
        this.code_input = '';
        return
      }
      if (res.length && res[0].bucketnumber) {
        var requestData = this.tableData;
        for (var i in requestData.bucket) {
          if(requestData.bucket[i].numberOfBarrels === res[0].bucketnumber){
            this.$message({
              message: "扫描失败，请勿重复扫描同一桶号",
              type: "error",
            });
            this.code_input = '';
            return
          }
        }
        this.tableData.bucket.push(res[0]);
        this.code_input = '';
      } else {
        this.code_input = '';
        this.$toast.fail('条码信息错误');
      }
    },

    //销售出库
    onSubmit() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
        this.postSalesOutbound();
      }, 1000);
      this.dialogVisible = false;
    },

    //过账接口
    async postSalesOutbound() {
      var requestData = this.tableData;
      var bucket = [];
      for (let i in requestData.bucket) {
        bucket.push({
          numberOfBarrels: requestData.bucket[i].numberOfBarrels,
        });
      }
      let inputInfo = {
        plant: localStorage.getItem("plant"),
        persnumber:localStorage.getItem("erpId"),
        deliveryDocument: requestData.deliverydocument,
        customerName: requestData.customername,
        totalQuantity: requestData.totalquantity,
        bucket: bucket,
      };
      console.log("inputInfo",inputInfo)
      const { data: res } = await this.$http.post(
          "/PostMaterialsStorageMateriel",
          inputInfo
      );
      var response = res[0];
      console.log("res",response)
      /*if (response.type == "S") {
        this.$toast.success(response.message);
        setTimeout(() => {
          this.$router.push({
            path: "/ScanView",
            query: { title: '销售出库扫描',key:6 },
          });
        }, 1000);
      } else if (response.type == "E") {
        this.tableData.info = response.message,
        this.$message({
          message: response.message,
          type: "error",
        });
      } else {
        this.$message({
          message: "出库失败，请联系系统维护人员",
          type: "error",
        });
      }*/
    },

  },
}
</script>

<style>
.el-input__inner {
  border: 0px;
  outline: none;
}
</style>